home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume4 / apml / part02 < prev    next >
Encoding:
Internet Message Format  |  1989-02-03  |  44.6 KB

  1. Path: xanth!mcnc!gatech!ukma!cwjcc!hal!ncoast!allbery
  2. From: ljz@fxgrp.UUCP (Lloyd Zusman)
  3. Newsgroups: comp.sources.misc
  4. Subject: v04i116: Arbitrary Precision Math Library -- 2 of 5
  5. Message-ID: <8810051944.AA05713@fxgrp.fx.com>
  6. Date: 7 Oct 88 00:13:25 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: ljz@fxgrp.UUCP (Lloyd Zusman)
  9. Lines: 1462
  10. Approved: allbery@ncoast.UUCP
  11.  
  12. Posting-number: Volume 4, Issue 116
  13. Submitted-by: "Lloyd Zusman" <ljz@fxgrp.UUCP>
  14. Archive-name: apml/Part2
  15.  
  16. Enclosed you will find the Arbitrary Precision Math Library (2 of 5)
  17.  
  18. Please post this to the comp.sources.misc newsgroup.
  19.  
  20. I finally got this into good enough shape to send out to the net. To use,
  21. just unshar the 5 pieces, read the README file, possibly alter the makefiles
  22. to conform to your system's conventions, and then type 'make test'.
  23.  
  24. Good luck!
  25.  
  26. --
  27.   Lloyd Zusman                  Internet:  ljz@fx.com
  28.   Master Byte Software                  or ljz%fx.com@ames.arc.nasa.gov
  29.   Los Gatos, California                 or fxgrp!ljz@ames.arc.nasa.gov
  30.   "We take things well in hand."    uucp:  ...!ames!fxgrp!ljz
  31.   [ our Internet connection is down: use uucp or mail to the entry above it ]
  32.  
  33. #--------------------------Cut Here--------------------------
  34. #! /bin/sh
  35. # This is a shell archive.  Remove anything before the "#! /bin/sh" line,
  36. # then unpack it by saving it in a file and typing "sh file."
  37. #
  38. # Wrapped by Lloyd Zusman (ljz) at fxgrp on Wed Oct  5 12:41:51 1988
  39. #
  40. # unpacks with default permissions
  41. #
  42. # Contents : LICENSE Makefile makefile.msc apm.arf addsub.c calc.c
  43. #
  44. if `test ! -s LICENSE`
  45. then
  46. echo "x - LICENSE"
  47. sed 's/^X//' > LICENSE << '@\END_OF_FILE_LICENSE'
  48. X    Arbitrary Precision Math Library General Public License
  49. X            (Written October 5, 1988)
  50. X
  51. X Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los
  52. X Gatos, California.  Everyone is permitted to copy and distribute
  53. X verbatim copies of this license, but changing it is not allowed.
  54. X You can also use this wording to make the terms for other programs.
  55. X
  56. X The wording of this license is based on that of the
  57. X "GNU EMACS GENERAL PUBLIC LICENSE" by Richard Stallman,
  58. X Copyright (C) 1985, 1987, 1988, version of February 11, 1988,
  59. X but since some of the text has been changed, please be sure to
  60. X READ THIS CAREFULLY!
  61. X
  62. X  This general public license is intended to give everyone the right
  63. Xto share the Arbitrary Precision Math Library (hereinafter referred to
  64. Xas the "APM Library").  To make sure that you get the rights we want
  65. Xyou to have, I need to make restrictions that forbid anyone to deny
  66. Xyou these rights or to ask you to surrender the rights.
  67. X
  68. X  Specifically, we want to make sure that you have the right to give
  69. Xaway copies of the APM Library, that you receive source code or else
  70. Xcan get it if you want it, that you can change the APM Library or use
  71. Xpieces of it in new programs, and that you know you can do these
  72. Xthings.
  73. X
  74. X  To make sure that everyone has such rights, we have to forbid you to
  75. Xdeprive anyone else of these rights.  For example, if you distribute
  76. Xcopies of the APM Library, you must give the recipients all the
  77. Xrights that you have.  You must make sure that they, too, receive or
  78. Xcan get the source code.  And you must tell them their rights.
  79. X
  80. X  Also, for our own protection, we must make certain that everyone
  81. Xfinds out that there is no warranty for the APM Library.  If the APM
  82. XLibrary is modified by someone else and passed on, we want its
  83. Xrecipients to know that what they have is not what we distributed, so
  84. Xthat any problems introduced by others will not reflect on our
  85. Xreputation.
  86. X
  87. X  Therefore we (Lloyd Zusman and Master Byte Software) make the
  88. Xfollowing terms which say what you must do to be allowed to
  89. Xdistribute or change the APM Library.
  90. X
  91. X            COPYING POLICIES
  92. X
  93. X1. You may copy and distribute verbatim copies of the APM Library
  94. Xsource code as you receive it, in any medium, provided that you
  95. Xconspicuously and appropriately publish on each copy a valid copyright
  96. Xnotice "Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los
  97. XGatos, California" (or with whatever year is appropriate); keep intact
  98. Xthe notices on all files that refer to this License Agreement and to
  99. Xthe absence of any warranty; and give any other recipients of the the
  100. XAPM Library program a copy of this License Agreement along with the
  101. Xprogram.  You may charge a distribution fee for the physical act of
  102. Xtransferring a copy.
  103. X
  104. X  2. You may modify your copy or copies of the APM Library source code or
  105. Xany portion of it, and copy and distribute such modifications under
  106. Xthe terms of Paragraph 1 above, provided that you also do the following:
  107. X
  108. X    a) cause the modified files to carry prominent notices stating
  109. X    that you changed the files and the date of any change; and
  110. X
  111. X    b) cause the whole of any work that you distribute or publish, that in
  112. X    whole or in part contains or is a derivative of the APM Library or any
  113. X    part thereof, to be licensed to all third parties on terms identical
  114. X    to those contained in this License Agreement (except that you may
  115. X    choose to grant more extensive warranty protection to some or all
  116. X    third parties, at your option).
  117. X
  118. X    c) You may charge a distribution fee for the physical act of
  119. X    transferring a copy, and you may at your option offer warranty
  120. X    protection in exchange for a fee.
  121. X
  122. X    d) You may not charge a license fee for the whole of any work that
  123. X    you distribute or publish, that in whole or in part contains or is
  124. X    a derivative of the APM library or any part thereof, without the
  125. X    express written permission of Lloyd Zusman and Master Byte Software;
  126. X    whether this permission is granted for free or in return for goods
  127. X    services, royalties, or other compensation will be determined
  128. X    solely by Lloyd Zusman and Master Byte Software.
  129. X
  130. XMere aggregation of another unrelated program with this program (or its
  131. Xderivative) on a volume of a storage or distribution medium does not bring
  132. Xthe other program under the scope of these terms.
  133. X
  134. X  3. You may copy and distribute the APM Library (or a portion or
  135. Xderivative of it, under Paragraph 2) in object code or executable form
  136. Xunder all the terms of Paragraphs 1 and 2 above provided that you also
  137. Xdo one of the following:
  138. X
  139. X    a) accompany it with the complete corresponding machine-readable
  140. X    source code, which must be distributed under the terms of
  141. X    Paragraphs 1 and 2 above; or,
  142. X
  143. X    b) accompany it with a written offer, valid for at least three
  144. X    years, to give any third party free (except for a nominal
  145. X    shipping charge) a complete machine-readable copy of the
  146. X    corresponding source code, to be distributed under the terms of
  147. X    Paragraphs 1 and 2 above; or,
  148. X
  149. X    c) accompany it with the information you received as to where the
  150. X    corresponding source code may be obtained.  (This alternative is
  151. X    allowed only for noncommercial distribution and only if you
  152. X    received the program in object code or executable form alone.)
  153. X
  154. XFor an executable file, complete source code means all the source code
  155. Xfor all modules it contains; but, as a special exception, it need not
  156. Xinclude source code for modules which are standard libraries that
  157. Xaccompany the operating system on which the executable file runs.
  158. X
  159. X  4. You may not copy, sublicense, distribute or transfer the APM
  160. XLibrary except as expressly provided under this License Agreement.
  161. XAny attempt otherwise to copy, sublicense, distribute or transfer the
  162. XAPM Library is void and your rights to use the APM Library under this
  163. XLicense agreement shall be automatically terminated.  However, parties
  164. Xwho have received computer software programs from you with this
  165. XLicense Agreement will not have their licenses terminated so long as
  166. Xsuch parties remain in full compliance.
  167. X
  168. X  5. If you wish to incorporate parts of the APM Library into other
  169. Xprograms whose distribution conditions are different, write to Lloyd
  170. XZusman at Master Byte Software.  We have not yet worked out a simple
  171. Xrule that can be stated here, but we will often permit this.  We will
  172. Xbe guided by the goals of (1) preserving the free status of all
  173. Xderivatives of our free software; of (2) promoting the sharing and
  174. Xreuse of software; and of (3) not allowing anyone to profit from the
  175. Xuse of our software without us also having the opportunity to share
  176. Xin these profits.
  177. X
  178. XYour comments and suggestions about our licensing policies and our
  179. Xsoftware are welcome!  Please contact Lloyd Zusman, Master Byte
  180. XSoftware, 127 Wilder Ave., Los Gatos, California 95030, or call
  181. X(408) 395-5693.
  182. X
  183. X               NO WARRANTY
  184. X
  185. X  BECAUSE THE APM LIBRARY IS LICENSED FREE OF CHARGE, WE PROVIDE
  186. XABSOLUTELY NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE
  187. XLAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING, MASTER BYTE SOFTWARE,
  188. XLLOYD ZUSMAN AND/OR OTHER PARTIES PROVIDE THE APM LIBRARY "AS IS"
  189. XWITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  190. XBUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  191. XFITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  192. XAND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE THE APM
  193. XLIBRARY PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY
  194. XSERVICING, REPAIR OR CORRECTION.
  195. X
  196. X  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL MASTER BYTE
  197. XSOFTWARE, LLOYD ZUSMAN, AND/OR ANY OTHER PARTY WHO MAY MODIFY AND
  198. XREDISTRIBUTE THE APM LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR
  199. XDAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL,
  200. XINCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
  201. XINABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA
  202. XBEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A
  203. XFAILURE OF THE PROGRAM TO OPERATE WITH PROGRAMS NOT DISTRIBUTED BY
  204. XMASTER BYTE SOFTWARE) THE PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF
  205. XTHE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  206. @\END_OF_FILE_LICENSE
  207. else
  208.   echo "shar: Will not over write LICENSE"
  209. fi
  210. if `test ! -s Makefile`
  211. then
  212. echo "x - Makefile"
  213. sed 's/^X//' > Makefile << '@\END_OF_FILE_Makefile'
  214. X#
  215. X# Makefile for building the APM library, "libapm.a"
  216. X#
  217. X
  218. XCFLAGS        = -O
  219. X
  220. XLIBRARY          = libapm.a
  221. X
  222. XOBJS          = addsub.o calc.o memory.o misc.o muldiv.o utils.o
  223. X
  224. XSRCS          = addsub.c calc.c memory.c misc.c muldiv.c utils.c
  225. X
  226. Xall:        $(LIBRARY)
  227. X
  228. X$(LIBRARY):    $(OBJS)
  229. X        ar cru $(LIBRARY) $(OBJS)
  230. X        ranlib $(LIBRARY)
  231. X
  232. Xtest:        test/apmtest
  233. X
  234. Xtest/apmtest:    $(LIBRARY)
  235. X        cd test; make
  236. X
  237. Xclean:
  238. X        rm -f $(OBJS)
  239. X        cd test; make clean
  240. X
  241. X# The 'depend' production assumes the use of a program called 'makedepend'.
  242. X# Change your 'depend' line accordingly.
  243. X
  244. Xdepend:
  245. X        makedepend $(SRCS)
  246. X        cd test ; make depend
  247. X        
  248. X# The 'shar' production assumes the use of a rather specific shar program
  249. X# called 'doshar'.  Change your 'shar' line accordingly.
  250. X
  251. Xshar:
  252. X        doshar README FUNCTIONS LICENSE Makefile makefile.msc apm.arf \
  253. X        *.c *.h test/Makefile test/makefile.msc test/apmtest.c
  254. X
  255. X###
  256. X# DO NOT DELETE THIS LINE -- makedepend uses it.
  257. X# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
  258. X
  259. Xaddsub.c:
  260. Xaddsub.o: addsub.c /usr/include/stdio.h apm.h apmlocal.h
  261. Xcalc.c:
  262. Xcalc.o: calc.c /usr/include/stdio.h /usr/include/varargs.h apm.h apmlocal.h
  263. Xmemory.c:
  264. Xmemory.o: memory.c /usr/include/stdio.h apm.h apmlocal.h
  265. Xmisc.c:
  266. Xmisc.o: misc.c /usr/include/stdio.h apm.h apmlocal.h
  267. Xmuldiv.c:
  268. Xmuldiv.o: muldiv.c /usr/include/stdio.h apm.h apmlocal.h
  269. Xutils.c:
  270. Xutils.o: utils.c /usr/include/stdio.h /usr/include/varargs.h apm.h apmlocal.h
  271. X
  272. X# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
  273. @\END_OF_FILE_Makefile
  274. else
  275.   echo "shar: Will not over write Makefile"
  276. fi
  277. if `test ! -s makefile.msc`
  278. then
  279. echo "x - makefile.msc"
  280. sed 's/^X//' > makefile.msc << '@\END_OF_FILE_makefile.msc'
  281. XM          = S
  282. X
  283. XCFLAGS          = -A$(M) -DMSC -Ox
  284. X
  285. XMAKEFILE      = makefile
  286. X
  287. XLIBRARY          = apm.lib
  288. X
  289. XRESPFILE      = apm.arf
  290. X
  291. XSRCS          = addsub.c \
  292. X        calc.c \
  293. X        memory.c \
  294. X        misc.c \
  295. X        muldiv.c \
  296. X        utils.c
  297. X
  298. XOBJS          = addsub.obj \
  299. X        calc.obj \
  300. X        memory.obj \
  301. X        misc.obj \
  302. X        muldiv.obj \
  303. X        utils.obj
  304. X
  305. X.c.obj:
  306. X        cl $(CFLAGS) -c $*.c
  307. X
  308. Xall:        $(LIBRARY)
  309. X
  310. X$(LIBRARY):    $(OBJS)
  311. X        @-rm -f $(LIBRARY)
  312. X        lib @$(RESPFILE)
  313. X
  314. Xclean:
  315. X        rm -f $(OBJS)
  316. X
  317. X###
  318. Xaddsub.obj: c:/ms/include/stdio.h apm.h c:/ms/include/malloc.h apmlocal.h
  319. Xcalc.obj: c:/ms/include/stdio.h c:/ms/include/varargs.h apm.h \
  320. X    c:/ms/include/malloc.h apmlocal.h
  321. Xmemory.obj: c:/ms/include/stdio.h apm.h c:/ms/include/malloc.h apmlocal.h
  322. Xmisc.obj: c:/ms/include/stdio.h apm.h c:/ms/include/malloc.h apmlocal.h
  323. Xmuldiv.obj: c:/ms/include/stdio.h apm.h c:/ms/include/malloc.h apmlocal.h
  324. Xutils.obj: c:/ms/include/stdio.h c:/ms/include/varargs.h apm.h \
  325. X    c:/ms/include/malloc.h apmlocal.h
  326. @\END_OF_FILE_makefile.msc
  327. else
  328.   echo "shar: Will not over write makefile.msc"
  329. fi
  330. if `test ! -s apm.arf`
  331. then
  332. echo "x - apm.arf"
  333. sed 's/^X//' > apm.arf << '@\END_OF_FILE_apm.arf'
  334. Xapm.lib
  335. Xy
  336. X+addsub.obj&
  337. X+calc.obj&
  338. X+memory.obj&
  339. X+misc.obj&
  340. X+muldiv.obj&
  341. X+utils.obj
  342. Xnul
  343. @\END_OF_FILE_apm.arf
  344. else
  345.   echo "shar: Will not over write apm.arf"
  346. fi
  347. if `test ! -s addsub.c`
  348. then
  349. echo "x - addsub.c"
  350. sed 's/^X//' > addsub.c << '@\END_OF_FILE_addsub.c'
  351. X/******************************************************************************
  352. X
  353. X    Arbitrary Precision Math Library General Public License
  354. X            (Written October 5, 1988)
  355. X
  356. X Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los
  357. X Gatos, California.  Everyone is permitted to copy and distribute
  358. X verbatim copies of this license, but changing it is not allowed.
  359. X You can also use this wording to make the terms for other programs.
  360. X
  361. X The wording of this license is based on that of the
  362. X "GNU EMACS GENERAL PUBLIC LICENSE" by Richard Stallman,
  363. X Copyright (C) 1985, 1987, 1988, version of February 11, 1988,
  364. X but since some of the text has been changed, please be sure to
  365. X READ THIS CAREFULLY!
  366. X
  367. X  This general public license is intended to give everyone the right
  368. Xto share the Arbitrary Precision Math Library (hereinafter referred to
  369. Xas the "APM Library").  To make sure that you get the rights we want
  370. Xyou to have, I need to make restrictions that forbid anyone to deny
  371. Xyou these rights or to ask you to surrender the rights.
  372. X
  373. X  Specifically, we want to make sure that you have the right to give
  374. Xaway copies of the APM Library, that you receive source code or else
  375. Xcan get it if you want it, that you can change the APM Library or use
  376. Xpieces of it in new programs, and that you know you can do these
  377. Xthings.
  378. X
  379. X  To make sure that everyone has such rights, we have to forbid you to
  380. Xdeprive anyone else of these rights.  For example, if you distribute
  381. Xcopies of the APM Library, you must give the recipients all the
  382. Xrights that you have.  You must make sure that they, too, receive or
  383. Xcan get the source code.  And you must tell them their rights.
  384. X
  385. X  Also, for our own protection, we must make certain that everyone
  386. Xfinds out that there is no warranty for the APM Library.  If the APM
  387. XLibrary is modified by someone else and passed on, we want its
  388. Xrecipients to know that what they have is not what we distributed, so
  389. Xthat any problems introduced by others will not reflect on our
  390. Xreputation.
  391. X
  392. X  Therefore we (Lloyd Zusman and Master Byte Software) make the
  393. Xfollowing terms which say what you must do to be allowed to
  394. Xdistribute or change the APM Library.
  395. X
  396. X            COPYING POLICIES
  397. X
  398. X1. You may copy and distribute verbatim copies of the APM Library
  399. Xsource code as you receive it, in any medium, provided that you
  400. Xconspicuously and appropriately publish on each copy a valid copyright
  401. Xnotice "Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los
  402. XGatos, California" (or with whatever year is appropriate); keep intact
  403. Xthe notices on all files that refer to this License Agreement and to
  404. Xthe absence of any warranty; and give any other recipients of the the
  405. XAPM Library program a copy of this License Agreement along with the
  406. Xprogram.  You may charge a distribution fee for the physical act of
  407. Xtransferring a copy.
  408. X
  409. X  2. You may modify your copy or copies of the APM Library source code or
  410. Xany portion of it, and copy and distribute such modifications under
  411. Xthe terms of Paragraph 1 above, provided that you also do the following:
  412. X
  413. X    a) cause the modified files to carry prominent notices stating
  414. X    that you changed the files and the date of any change; and
  415. X
  416. X    b) cause the whole of any work that you distribute or publish, that in
  417. X    whole or in part contains or is a derivative of the APM Library or any
  418. X    part thereof, to be licensed to all third parties on terms identical
  419. X    to those contained in this License Agreement (except that you may
  420. X    choose to grant more extensive warranty protection to some or all
  421. X    third parties, at your option).
  422. X
  423. X    c) You may charge a distribution fee for the physical act of
  424. X    transferring a copy, and you may at your option offer warranty
  425. X    protection in exchange for a fee.
  426. X
  427. X    d) You may not charge a license fee for the whole of any work that
  428. X    you distribute or publish, that in whole or in part contains or is
  429. X    a derivative of the APM library or any part thereof, without the
  430. X    express written permission of Lloyd Zusman and Master Byte Software;
  431. X    whether this permission is granted for free or in return for goods
  432. X    services, royalties, or other compensation will be determined
  433. X    solely by Lloyd Zusman and Master Byte Software.
  434. X
  435. XMere aggregation of another unrelated program with this program (or its
  436. Xderivative) on a volume of a storage or distribution medium does not bring
  437. Xthe other program under the scope of these terms.
  438. X
  439. X  3. You may copy and distribute the APM Library (or a portion or
  440. Xderivative of it, under Paragraph 2) in object code or executable form
  441. Xunder all the terms of Paragraphs 1 and 2 above provided that you also
  442. Xdo one of the following:
  443. X
  444. X    a) accompany it with the complete corresponding machine-readable
  445. X    source code, which must be distributed under the terms of
  446. X    Paragraphs 1 and 2 above; or,
  447. X
  448. X    b) accompany it with a written offer, valid for at least three
  449. X    years, to give any third party free (except for a nominal
  450. X    shipping charge) a complete machine-readable copy of the
  451. X    corresponding source code, to be distributed under the terms of
  452. X    Paragraphs 1 and 2 above; or,
  453. X
  454. X    c) accompany it with the information you received as to where the
  455. X    corresponding source code may be obtained.  (This alternative is
  456. X    allowed only for noncommercial distribution and only if you
  457. X    received the program in object code or executable form alone.)
  458. X
  459. XFor an executable file, complete source code means all the source code
  460. Xfor all modules it contains; but, as a special exception, it need not
  461. Xinclude source code for modules which are standard libraries that
  462. Xaccompany the operating system on which the executable file runs.
  463. X
  464. X  4. You may not copy, sublicense, distribute or transfer the APM
  465. XLibrary except as expressly provided under this License Agreement.
  466. XAny attempt otherwise to copy, sublicense, distribute or transfer the
  467. XAPM Library is void and your rights to use the APM Library under this
  468. XLicense agreement shall be automatically terminated.  However, parties
  469. Xwho have received computer software programs from you with this
  470. XLicense Agreement will not have their licenses terminated so long as
  471. Xsuch parties remain in full compliance.
  472. X
  473. X  5. If you wish to incorporate parts of the APM Library into other
  474. Xprograms whose distribution conditions are different, write to Lloyd
  475. XZusman at Master Byte Software.  We have not yet worked out a simple
  476. Xrule that can be stated here, but we will often permit this.  We will
  477. Xbe guided by the goals of (1) preserving the free status of all
  478. Xderivatives of our free software; of (2) promoting the sharing and
  479. Xreuse of software; and of (3) not allowing anyone to profit from the
  480. Xuse of our software without us also having the opportunity to share
  481. Xin these profits.
  482. X
  483. XYour comments and suggestions about our licensing policies and our
  484. Xsoftware are welcome!  Please contact Lloyd Zusman, Master Byte
  485. XSoftware, 127 Wilder Ave., Los Gatos, California 95030, or call
  486. X(408) 395-5693.
  487. X
  488. X               NO WARRANTY
  489. X
  490. X  BECAUSE THE APM LIBRARY IS LICENSED FREE OF CHARGE, WE PROVIDE
  491. XABSOLUTELY NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE
  492. XLAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING, MASTER BYTE SOFTWARE,
  493. XLLOYD ZUSMAN AND/OR OTHER PARTIES PROVIDE THE APM LIBRARY "AS IS"
  494. XWITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  495. XBUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  496. XFITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  497. XAND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE THE APM
  498. XLIBRARY PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY
  499. XSERVICING, REPAIR OR CORRECTION.
  500. X
  501. X  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL MASTER BYTE
  502. XSOFTWARE, LLOYD ZUSMAN, AND/OR ANY OTHER PARTY WHO MAY MODIFY AND
  503. XREDISTRIBUTE THE APM LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR
  504. XDAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL,
  505. XINCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
  506. XINABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA
  507. XBEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A
  508. XFAILURE OF THE PROGRAM TO OPERATE WITH PROGRAMS NOT DISTRIBUTED BY
  509. XMASTER BYTE SOFTWARE) THE PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF
  510. XTHE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  511. X
  512. X******************************************************************************/
  513. X
  514. X
  515. X/*
  516. X * Addition and subtraction routines for the APM library.
  517. X *
  518. X * $Log:    addsub.c,v $
  519. X * Revision 1.0  88/10/05  12:38:08  ljz
  520. X * Initial release.
  521. X * 
  522. X */
  523. X#ifndef lint
  524. Xstatic char rcsid[] = "$Header: addsub.c,v 1.0 88/10/05 12:38:08 ljz Exp $";
  525. X#endif /* ! lint */
  526. X
  527. X#include <stdio.h>
  528. X#include "apm.h"
  529. X#include "apmlocal.h"
  530. X
  531. Xint
  532. Xapm_add(result, num1, num2)
  533. XAPM result;
  534. XAPM num1;
  535. XAPM num2;
  536. X{
  537. X    int length1;
  538. X    int length2;
  539. X    int leftofdp1;
  540. X    int leftofdp2;
  541. X    int len;
  542. X    int dp;
  543. X    int n;
  544. X    int n1;
  545. X    int n2;
  546. X    int ercode;
  547. X    short sign;
  548. X    short base;
  549. X    short tempval;
  550. X    short carry;
  551. X    short c1;
  552. X    short c2;
  553. X
  554. X    apm_errno = APM_OK;
  555. X
  556. X    ERR_RETURN(APM_val_format(result));
  557. X    ERR_RETURN(apm_validate(num1));
  558. X    ERR_RETURN(apm_validate(num2));
  559. X
  560. X    if (result == num1 || result == num2) {
  561. X        return (APM_error(APM_EOVERLAP));
  562. X    }
  563. X    if (num1->base != num2->base) {
  564. X        return (APM_error(APM_EBASE));
  565. X    }
  566. X    base = num1->base;
  567. X
  568. X    if (num1->sign < 0 && num2->sign >= 0) {
  569. X        num1->sign = 1;
  570. X        ercode = apm_subtract(result, num2, num1);
  571. X        num1->sign = -1;
  572. X        return (APM_error(ercode));
  573. X    }
  574. X    else if (num1->sign >= 0 && num2->sign < 0) {
  575. X        num2->sign = 1;
  576. X        ercode = apm_subtract(result, num1, num2);
  577. X        num2->sign = -1;
  578. X        return (APM_error(ercode));
  579. X    }
  580. X
  581. X    sign = SIGNOF(num1->sign);
  582. X
  583. X    ERR_RETURN(APM_trim(num1, 1, 1));
  584. X    ERR_RETURN(APM_trim(num2, 1, 1));
  585. X
  586. X    length1 = num1->length;
  587. X    length2 = num2->length;
  588. X    
  589. X    if (num1->dp >= num2->dp) {
  590. X        dp = num1->dp;
  591. X        n1 = 0;
  592. X        n2 = num2->dp - num1->dp;
  593. X    }
  594. X    else {
  595. X        dp = num2->dp;
  596. X        n1 = num1->dp - num2->dp;
  597. X        n2 = 0;
  598. X    }
  599. X
  600. X    leftofdp1 = length1 - num1->dp;
  601. X    leftofdp2 = length2 - num2->dp;
  602. X
  603. X    if (leftofdp1 >= leftofdp2) {
  604. X        len = leftofdp1 + dp + 1;
  605. X    }
  606. X    else {
  607. X        len = leftofdp2 + dp + 1;
  608. X    }
  609. X
  610. X    ERR_RETURN(APM_size(result, len));
  611. X
  612. X    carry = 0;
  613. X    for (n = 0; n < len - 1; ++n, ++n1, ++n2) {
  614. X        if (n1 < 0 || n1 >= length1) {
  615. X            c1 = 0;
  616. X        }
  617. X        else {
  618. X            c1 = num1->data[n1];
  619. X        }
  620. X        if (n2 < 0 || n2 >= length2) {
  621. X            c2 = 0;
  622. X        }
  623. X        else {
  624. X            c2 = num2->data[n2];
  625. X        }
  626. X        tempval = c1 + c2 + carry;
  627. X        result->data[n] = tempval % base;
  628. X        carry = tempval / base;
  629. X    }
  630. X
  631. X    result->data[n] = carry;
  632. X    result->base = base;
  633. X    result->length = len;
  634. X    result->sign = sign;
  635. X    result->dp = dp;
  636. X
  637. X    return (APM_error(APM_trim(result, 1, 1)));
  638. X}
  639. X
  640. Xint
  641. Xapm_subtract(result, num1, num2)
  642. XAPM result;
  643. XAPM num1;
  644. XAPM num2;
  645. X{
  646. X    int length1;
  647. X    int length2;
  648. X    int leftofdp1;
  649. X    int leftofdp2;
  650. X    int len;
  651. X    int dp;
  652. X    int n;
  653. X    int n1;
  654. X    int n2;
  655. X    int ercode;
  656. X    short base;
  657. X    short tempval;
  658. X    short borrow;
  659. X    short c1;
  660. X    short c2;
  661. X
  662. X    apm_errno = APM_OK;
  663. X
  664. X    ERR_RETURN(APM_val_format(result));
  665. X    ERR_RETURN(apm_validate(num1));
  666. X    ERR_RETURN(apm_validate(num2));
  667. X
  668. X    if (result == num1 || result == num2) {
  669. X        return (APM_error(APM_EOVERLAP));
  670. X    }
  671. X    if (num1->base != num2->base) {
  672. X        return (APM_error(APM_EBASE));
  673. X    }
  674. X
  675. X    base = num1->base;
  676. X
  677. X    if (num1->sign < 0 && num2->sign >= 0) {
  678. X        num1->sign = 1;
  679. X        ercode = apm_add(result, num2, num1);
  680. X        num1->sign = -1;
  681. X        if (ercode >= APM_OK) {
  682. X            result->sign = -(result->sign);
  683. X        }
  684. X        return (APM_error(ercode));
  685. X    }
  686. X    else if (num1->sign >= 0 && num2->sign < 0) {
  687. X        num2->sign = 1;
  688. X        ercode = apm_add(result, num1, num2);
  689. X        num2->sign = -1;
  690. X        return (APM_error(ercode));
  691. X    }
  692. X
  693. X    ERR_RETURN(APM_trim(num1, 1, 1));
  694. X    ERR_RETURN(APM_trim(num2, 1, 1));
  695. X
  696. X    length1 = num1->length;
  697. X    length2 = num2->length;
  698. X    
  699. X    if (num1->dp >= num2->dp) {
  700. X        dp = num1->dp;
  701. X        n1 = 0;
  702. X        n2 = num2->dp - num1->dp;
  703. X    }
  704. X    else {
  705. X        dp = num2->dp;
  706. X        n1 = num1->dp - num2->dp;
  707. X        n2 = 0;
  708. X    }
  709. X
  710. X    leftofdp1 = length1 - num1->dp;
  711. X    leftofdp2 = length2 - num2->dp;
  712. X
  713. X    if (leftofdp1 >= leftofdp2) {
  714. X        len = leftofdp1 + dp + 1;
  715. X    }
  716. X    else {
  717. X        len = leftofdp2 + dp + 1;
  718. X    }
  719. X
  720. X    ERR_RETURN(APM_size(result, len));
  721. X
  722. X    borrow = 0;
  723. X    for (n = 0; n < len - 1; ++n, ++n1, ++n2) {
  724. X        if (n1 < 0 || n1 >= length1) {
  725. X            c1 = 0;
  726. X        }
  727. X        else {
  728. X            c1 = num1->data[n1];
  729. X        }
  730. X        if (n2 < 0 || n2 >= length2) {
  731. X            c2 = 0;
  732. X        }
  733. X        else {
  734. X            c2 = num2->data[n2];
  735. X        }
  736. X        tempval = (c1 + borrow) - c2;
  737. X        if (tempval < 0) {
  738. X            borrow = -1;
  739. X            tempval += base;
  740. X        }
  741. X        else {
  742. X            borrow = 0;
  743. X        }
  744. X        result->data[n] = tempval;
  745. X    }
  746. X
  747. X    if (borrow == 0) {
  748. X        result->data[n] = 0;
  749. X        result->length = len;
  750. X        result->sign = 1;
  751. X    }
  752. X    else {
  753. X        int carry = 1;
  754. X        int basem1 = base - 1;
  755. X        result->data[n] = basem1;
  756. X        for (n = 0; n < len; ++n) {
  757. X            int value = result->data[n];
  758. X            value = (basem1 - value) + carry;
  759. X            result->data[n] = value % base;
  760. X            carry = value / base;
  761. X        }
  762. X        result->sign = -1;
  763. X    }
  764. X
  765. X    if (num1->sign < 0) {
  766. X        result->sign = -(result->sign);
  767. X    }
  768. X
  769. X    result->length = len;
  770. X    result->base = base;
  771. X    result->dp = dp;
  772. X
  773. X    return (APM_error(APM_trim(result, 1, 1)));
  774. X}
  775. @\END_OF_FILE_addsub.c
  776. else
  777.   echo "shar: Will not over write addsub.c"
  778. fi
  779. if `test ! -s calc.c`
  780. then
  781. echo "x - calc.c"
  782. sed 's/^X//' > calc.c << '@\END_OF_FILE_calc.c'
  783. X/******************************************************************************
  784. X
  785. X    Arbitrary Precision Math Library General Public License
  786. X            (Written October 5, 1988)
  787. X
  788. X Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los
  789. X Gatos, California.  Everyone is permitted to copy and distribute
  790. X verbatim copies of this license, but changing it is not allowed.
  791. X You can also use this wording to make the terms for other programs.
  792. X
  793. X The wording of this license is based on that of the
  794. X "GNU EMACS GENERAL PUBLIC LICENSE" by Richard Stallman,
  795. X Copyright (C) 1985, 1987, 1988, version of February 11, 1988,
  796. X but since some of the text has been changed, please be sure to
  797. X READ THIS CAREFULLY!
  798. X
  799. X  This general public license is intended to give everyone the right
  800. Xto share the Arbitrary Precision Math Library (hereinafter referred to
  801. Xas the "APM Library").  To make sure that you get the rights we want
  802. Xyou to have, I need to make restrictions that forbid anyone to deny
  803. Xyou these rights or to ask you to surrender the rights.
  804. X
  805. X  Specifically, we want to make sure that you have the right to give
  806. Xaway copies of the APM Library, that you receive source code or else
  807. Xcan get it if you want it, that you can change the APM Library or use
  808. Xpieces of it in new programs, and that you know you can do these
  809. Xthings.
  810. X
  811. X  To make sure that everyone has such rights, we have to forbid you to
  812. Xdeprive anyone else of these rights.  For example, if you distribute
  813. Xcopies of the APM Library, you must give the recipients all the
  814. Xrights that you have.  You must make sure that they, too, receive or
  815. Xcan get the source code.  And you must tell them their rights.
  816. X
  817. X  Also, for our own protection, we must make certain that everyone
  818. Xfinds out that there is no warranty for the APM Library.  If the APM
  819. XLibrary is modified by someone else and passed on, we want its
  820. Xrecipients to know that what they have is not what we distributed, so
  821. Xthat any problems introduced by others will not reflect on our
  822. Xreputation.
  823. X
  824. X  Therefore we (Lloyd Zusman and Master Byte Software) make the
  825. Xfollowing terms which say what you must do to be allowed to
  826. Xdistribute or change the APM Library.
  827. X
  828. X            COPYING POLICIES
  829. X
  830. X1. You may copy and distribute verbatim copies of the APM Library
  831. Xsource code as you receive it, in any medium, provided that you
  832. Xconspicuously and appropriately publish on each copy a valid copyright
  833. Xnotice "Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los
  834. XGatos, California" (or with whatever year is appropriate); keep intact
  835. Xthe notices on all files that refer to this License Agreement and to
  836. Xthe absence of any warranty; and give any other recipients of the the
  837. XAPM Library program a copy of this License Agreement along with the
  838. Xprogram.  You may charge a distribution fee for the physical act of
  839. Xtransferring a copy.
  840. X
  841. X  2. You may modify your copy or copies of the APM Library source code or
  842. Xany portion of it, and copy and distribute such modifications under
  843. Xthe terms of Paragraph 1 above, provided that you also do the following:
  844. X
  845. X    a) cause the modified files to carry prominent notices stating
  846. X    that you changed the files and the date of any change; and
  847. X
  848. X    b) cause the whole of any work that you distribute or publish, that in
  849. X    whole or in part contains or is a derivative of the APM Library or any
  850. X    part thereof, to be licensed to all third parties on terms identical
  851. X    to those contained in this License Agreement (except that you may
  852. X    choose to grant more extensive warranty protection to some or all
  853. X    third parties, at your option).
  854. X
  855. X    c) You may charge a distribution fee for the physical act of
  856. X    transferring a copy, and you may at your option offer warranty
  857. X    protection in exchange for a fee.
  858. X
  859. X    d) You may not charge a license fee for the whole of any work that
  860. X    you distribute or publish, that in whole or in part contains or is
  861. X    a derivative of the APM library or any part thereof, without the
  862. X    express written permission of Lloyd Zusman and Master Byte Software;
  863. X    whether this permission is granted for free or in return for goods
  864. X    services, royalties, or other compensation will be determined
  865. X    solely by Lloyd Zusman and Master Byte Software.
  866. X
  867. XMere aggregation of another unrelated program with this program (or its
  868. Xderivative) on a volume of a storage or distribution medium does not bring
  869. Xthe other program under the scope of these terms.
  870. X
  871. X  3. You may copy and distribute the APM Library (or a portion or
  872. Xderivative of it, under Paragraph 2) in object code or executable form
  873. Xunder all the terms of Paragraphs 1 and 2 above provided that you also
  874. Xdo one of the following:
  875. X
  876. X    a) accompany it with the complete corresponding machine-readable
  877. X    source code, which must be distributed under the terms of
  878. X    Paragraphs 1 and 2 above; or,
  879. X
  880. X    b) accompany it with a written offer, valid for at least three
  881. X    years, to give any third party free (except for a nominal
  882. X    shipping charge) a complete machine-readable copy of the
  883. X    corresponding source code, to be distributed under the terms of
  884. X    Paragraphs 1 and 2 above; or,
  885. X
  886. X    c) accompany it with the information you received as to where the
  887. X    corresponding source code may be obtained.  (This alternative is
  888. X    allowed only for noncommercial distribution and only if you
  889. X    received the program in object code or executable form alone.)
  890. X
  891. XFor an executable file, complete source code means all the source code
  892. Xfor all modules it contains; but, as a special exception, it need not
  893. Xinclude source code for modules which are standard libraries that
  894. Xaccompany the operating system on which the executable file runs.
  895. X
  896. X  4. You may not copy, sublicense, distribute or transfer the APM
  897. XLibrary except as expressly provided under this License Agreement.
  898. XAny attempt otherwise to copy, sublicense, distribute or transfer the
  899. XAPM Library is void and your rights to use the APM Library under this
  900. XLicense agreement shall be automatically terminated.  However, parties
  901. Xwho have received computer software programs from you with this
  902. XLicense Agreement will not have their licenses terminated so long as
  903. Xsuch parties remain in full compliance.
  904. X
  905. X  5. If you wish to incorporate parts of the APM Library into other
  906. Xprograms whose distribution conditions are different, write to Lloyd
  907. XZusman at Master Byte Software.  We have not yet worked out a simple
  908. Xrule that can be stated here, but we will often permit this.  We will
  909. Xbe guided by the goals of (1) preserving the free status of all
  910. Xderivatives of our free software; of (2) promoting the sharing and
  911. Xreuse of software; and of (3) not allowing anyone to profit from the
  912. Xuse of our software without us also having the opportunity to share
  913. Xin these profits.
  914. X
  915. XYour comments and suggestions about our licensing policies and our
  916. Xsoftware are welcome!  Please contact Lloyd Zusman, Master Byte
  917. XSoftware, 127 Wilder Ave., Los Gatos, California 95030, or call
  918. X(408) 395-5693.
  919. X
  920. X               NO WARRANTY
  921. X
  922. X  BECAUSE THE APM LIBRARY IS LICENSED FREE OF CHARGE, WE PROVIDE
  923. XABSOLUTELY NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE
  924. XLAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING, MASTER BYTE SOFTWARE,
  925. XLLOYD ZUSMAN AND/OR OTHER PARTIES PROVIDE THE APM LIBRARY "AS IS"
  926. XWITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  927. XBUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  928. XFITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  929. XAND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE THE APM
  930. XLIBRARY PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY
  931. XSERVICING, REPAIR OR CORRECTION.
  932. X
  933. X  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL MASTER BYTE
  934. XSOFTWARE, LLOYD ZUSMAN, AND/OR ANY OTHER PARTY WHO MAY MODIFY AND
  935. XREDISTRIBUTE THE APM LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR
  936. XDAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL,
  937. XINCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
  938. XINABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA
  939. XBEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A
  940. XFAILURE OF THE PROGRAM TO OPERATE WITH PROGRAMS NOT DISTRIBUTED BY
  941. XMASTER BYTE SOFTWARE) THE PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF
  942. XTHE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  943. X
  944. X******************************************************************************/
  945. X
  946. X
  947. X/*
  948. X * Calculator mode for the APM library.
  949. X *
  950. X * $Log:    calc.c,v $
  951. X * Revision 1.0  88/10/05  12:38:11  ljz
  952. X * Initial release.
  953. X * 
  954. X */
  955. X#ifndef lint
  956. Xstatic char rcsid[] = "$Header: calc.c,v 1.0 88/10/05 12:38:11 ljz Exp $";
  957. X#endif /* ! lint */
  958. X
  959. X#include <stdio.h>
  960. X#include <varargs.h>
  961. X#include "apm.h"
  962. X#include "apmlocal.h"
  963. X
  964. X/*
  965. X * The stack is declared to contain one more value than is used so that
  966. X * the calculation of &APM_stack[APM_STACK_SIZE] doesn't generate an
  967. X * error (I have been led to believe that under certain machine
  968. X * architectures, calculating an invalid pointer will cause an error
  969. X * even if the pointer is not dereferenced).
  970. X */
  971. Xstatic APM APM_stack[APM_STACK_SIZE + 1];
  972. Xstatic APM *APM_stack_ptr = (APM *)NULL;
  973. X
  974. X/*
  975. X * We need a few scratch registers.  The user can't get to them, but
  976. X * I use them in some of these routines.
  977. X */
  978. X#define NUM_SCRATCH    3
  979. X#define TOT_REGISTERS    (APM_NUM_REGISTERS + NUM_SCRATCH)
  980. Xstatic APM APM_register[TOT_REGISTERS];
  981. X
  982. X/*
  983. X * This macro is used to access the scratch registers.
  984. X */
  985. X#define XREG(N)        APM_register[APM_NUM_REGISTERS + (N)]
  986. X
  987. Xstatic int
  988. XAPM_get_reg(value, regnum)
  989. XAPM value;
  990. Xint regnum;
  991. X{
  992. X    APM reg;
  993. X    int ercode = APM_OK;
  994. X
  995. X    APM_calc_init();
  996. X
  997. X    apm_errno = APM_OK;
  998. X
  999. X    if (regnum < 0 || regnum >= APM_NUM_REGISTERS) {
  1000. X        return (APM_set_errno(APM_EPARM));
  1001. X    }
  1002. X
  1003. X    reg = APM_register[regnum];
  1004. X    ercode = APM_size(value, reg->length);
  1005. X    if (ercode < APM_OK) {
  1006. X        return (APM_set_errno(ercode));
  1007. X    }
  1008. X
  1009. X    APM_copy_bytes(value->data, reg->data, reg->length * sizeof(short));
  1010. X    value->sign = SIGNOF(reg->sign);
  1011. X    value->base = reg->base;
  1012. X    value->length = reg->length;
  1013. X    value->dp = reg->dp;
  1014. X    return (APM_OK);
  1015. X}
  1016. X
  1017. Xstatic int
  1018. XAPM_set_reg(previous, regnum, value)
  1019. XAPM previous;
  1020. Xint regnum;
  1021. XAPM value;
  1022. X{
  1023. X    APM reg;
  1024. X    int ercode = APM_OK;
  1025. X
  1026. X    APM_calc_init();
  1027. X
  1028. X    apm_errno = APM_OK;
  1029. X
  1030. X    if (regnum < 0 || regnum >= APM_NUM_REGISTERS) {
  1031. X        return (APM_set_errno(APM_EPARM));
  1032. X    }
  1033. X
  1034. X    
  1035. X    ercode = apm_validate(value);
  1036. X    if (ercode < APM_OK) {
  1037. X        return (APM_set_errno(ercode));
  1038. X    }
  1039. X
  1040. X    if (previous == value) {
  1041. X        return (APM_set_errno(APM_EOVERLAP));
  1042. X    }
  1043. X
  1044. X    ercode = APM_get_reg(previous, regnum);
  1045. X    if (ercode < APM_OK) {
  1046. X        return (APM_set_errno(ercode));
  1047. X    }
  1048. X
  1049. X    reg = APM_register[regnum];
  1050. X    ercode = APM_size(reg, value->length);
  1051. X    if (ercode < APM_OK) {
  1052. X        return (APM_set_errno(ercode));
  1053. X    }
  1054. X
  1055. X    APM_copy_bytes(reg->data, value->data, value->length * sizeof(short));
  1056. X    reg->sign = SIGNOF(value->sign);
  1057. X    reg->base = value->base;
  1058. X    reg->length = value->length;
  1059. X    reg->dp = value->dp;
  1060. X    return (APM_OK);
  1061. X}
  1062. X
  1063. Xstatic int
  1064. XAPM_calc_op(value)
  1065. XAPM value;
  1066. X{
  1067. X    int ercode = APM_OK;
  1068. X
  1069. X    APM_calc_init();
  1070. X
  1071. X    if (value == (APM)NULL) {
  1072. X        return (APM_ENULL);
  1073. X    }
  1074. X    else if (value->magic != OP_MAGIC) {
  1075. X        return (APM_EBADVAL);
  1076. X    }
  1077. X
  1078. X    switch (value->length) {    /* length = command, dp = operand */
  1079. X    case APM_CALC_ABS:        /* unary operations */
  1080. X    case APM_CALC_NEG:
  1081. X    case APM_CALC_RECIP:
  1082. X    case APM_CALC_SCALE:
  1083. X        ercode = APM_calc_unop(value->length, value->dp);
  1084. X        break;
  1085. X    case APM_CALC_ADD:        /* binary operations */
  1086. X    case APM_CALC_SUB:
  1087. X    case APM_CALC_MUL:
  1088. X    case APM_CALC_DIV:
  1089. X        ercode = APM_calc_binop(value->length, value->dp);
  1090. X        break;
  1091. X    case APM_CALC_CLEAR:    /* miscellaneous operations */
  1092. X    case APM_CALC_DUP:
  1093. X    case APM_CALC_SWAP:
  1094. X    case APM_CALC_PUSH:
  1095. X    case APM_CALC_POP:
  1096. X        ercode = APM_calc_misc(value->length, value->dp);
  1097. X        break;
  1098. X    default:
  1099. X        ercode = APM_EBADVAL;
  1100. X        break;
  1101. X    }
  1102. X
  1103. X    return (ercode);
  1104. X}
  1105. X
  1106. Xstatic int
  1107. XAPM_calc_unop(operation, operand)
  1108. Xint operation;
  1109. Xint operand;
  1110. X{
  1111. X    int ercode = APM_OK;
  1112. X
  1113. X    APM_calc_init();
  1114. X
  1115. X    switch (operation) {
  1116. X    case APM_CALC_ABS:
  1117. X        ercode = APM_pop(XREG(0));
  1118. X        if (ercode != APM_OK) {
  1119. X            break;
  1120. X        }
  1121. X        ercode = apm_absolute_value(XREG(1), XREG(0));
  1122. X        if (ercode < APM_OK) {
  1123. X            APM_push(XREG(0));
  1124. X        }
  1125. X        else {
  1126. X            ercode = APM_push(XREG(1));
  1127. X        }
  1128. X        break;
  1129. X    case APM_CALC_NEG:
  1130. X        ercode = APM_pop(XREG(0));
  1131. X        if (ercode != APM_OK) {
  1132. X            break;
  1133. X        }
  1134. X        ercode = apm_negate(XREG(1), XREG(0));
  1135. X        if (ercode < APM_OK) {
  1136. X            APM_push(XREG(0));
  1137. X        }
  1138. X        else {
  1139. X            ercode = APM_push(XREG(1));
  1140. X        }
  1141. X        break;
  1142. X    case APM_CALC_RECIP:
  1143. X        ercode = APM_pop(XREG(0));
  1144. X        if (ercode != APM_OK) {
  1145. X            break;
  1146. X        }
  1147. X        ercode = apm_reciprocal(XREG(1), operand, XREG(0));
  1148. X        if (ercode < APM_OK) {
  1149. X            APM_push(XREG(0));
  1150. X        }
  1151. X        else {
  1152. X            ercode = APM_push(XREG(1));
  1153. X        }
  1154. X        break;
  1155. X    case APM_CALC_SCALE:
  1156. X        ercode = APM_pop(XREG(0));
  1157. X        if (ercode != APM_OK) {
  1158. X            break;
  1159. X        }
  1160. X        ercode = apm_scale(XREG(1), XREG(0), operand);
  1161. X        if (ercode < APM_OK) {
  1162. X            APM_push(XREG(0));
  1163. X        }
  1164. X        else {
  1165. X            ercode = APM_push(XREG(1));
  1166. X        }
  1167. X        break;
  1168. X    default:
  1169. X        ercode = APM_EBADVAL;
  1170. X        break;
  1171. X    }
  1172. X
  1173. X    return (APM_set_errno(ercode));
  1174. X}
  1175. X
  1176. Xstatic int
  1177. XAPM_calc_binop(operation, operand)
  1178. Xint operation;
  1179. Xint operand;
  1180. X{
  1181. X    int ercode = APM_OK;
  1182. X
  1183. X    APM_calc_init();
  1184. X
  1185. X    switch (operation) {
  1186. X    case APM_CALC_ADD:
  1187. X        APM_pop(XREG(0));
  1188. X        APM_pop(XREG(1));
  1189. X        apm_assign_long(XREG(2), 0L, 0, 0);
  1190. X        ercode = apm_add(XREG(2), XREG(1), XREG(0));
  1191. X        APM_push(XREG(2));
  1192. X        break;
  1193. X    case APM_CALC_SUB:
  1194. X        APM_pop(XREG(0));
  1195. X        APM_pop(XREG(1));
  1196. X        apm_assign_long(XREG(2), 0L, 0, 0);
  1197. X        ercode = apm_subtract(XREG(2), XREG(1), XREG(0));
  1198. X        APM_push(XREG(2));
  1199. X        break;
  1200. X    case APM_CALC_MUL:
  1201. X        APM_pop(XREG(0));
  1202. X        APM_pop(XREG(1));
  1203. X        apm_assign_long(XREG(2), 0L, 0, 0);
  1204. X        ercode = apm_multiply(XREG(2), XREG(1), XREG(0));
  1205. X        APM_push(XREG(2));
  1206. X        break;
  1207. X    case APM_CALC_DIV:
  1208. X        APM_pop(XREG(0));
  1209. X        APM_pop(XREG(1));
  1210. X        apm_assign_long(XREG(2), 0L, 0, 0);
  1211. X        ercode = apm_divide(XREG(2), operand, APM_register[0],
  1212. X                    XREG(1), XREG(0));
  1213. X        APM_push(XREG(2));
  1214. X        break;
  1215. X    default:
  1216. X        ercode = APM_EBADVAL;
  1217. X        break;
  1218. X    }
  1219. X
  1220. X    return (APM_set_errno(ercode));
  1221. X}
  1222. X
  1223. Xstatic int
  1224. XAPM_calc_misc(operation, operand)
  1225. Xint operation;
  1226. Xint operand;
  1227. X{
  1228. X    int ercode = APM_OK;
  1229. X
  1230. X    APM_calc_init();
  1231. X
  1232. X    switch (operation) {
  1233. X    case APM_CALC_CLEAR:
  1234. X        APM_stack_ptr = &APM_stack[0];
  1235. X        break;
  1236. X    case APM_CALC_DUP:
  1237. X        if ((ercode = APM_pop(XREG(0))) == APM_OK) {
  1238. X            if ((ercode = APM_push(XREG(0))) == APM_OK) {
  1239. X                ercode = APM_push(XREG(0));
  1240. X            }
  1241. X        }
  1242. X        break;
  1243. X    case APM_CALC_SWAP:
  1244. X        if ((ercode = APM_pop(XREG(0))) == APM_OK) {
  1245. X            if ((ercode = APM_pop(XREG(1))) == APM_OK) {
  1246. X                if ((ercode = APM_push(XREG(0))) == APM_OK) {
  1247. X                    ercode = APM_push(XREG(1));
  1248. X                }
  1249. X            }
  1250. X        }
  1251. X        break;
  1252. X    case APM_CALC_PUSH:
  1253. X        if (operand < 0 || operand >= APM_NUM_REGISTERS) {
  1254. X            ercode = APM_EPARM;
  1255. X            break;
  1256. X        }
  1257. X        ercode = APM_push(APM_register[operand]);
  1258. X        break;
  1259. X    case APM_CALC_POP:
  1260. X        if (operand < 0 || operand >= APM_NUM_REGISTERS) {
  1261. X            ercode = APM_EPARM;
  1262. X            break;
  1263. X        }
  1264. X        ercode = APM_pop(APM_register[operand]);
  1265. X        break;
  1266. X    default:
  1267. X        ercode = APM_EBADVAL;
  1268. X        break;
  1269. X    }
  1270. X
  1271. X    return (APM_set_errno(ercode));
  1272. X}
  1273. X
  1274. Xstatic int
  1275. XAPM_push(value)
  1276. XAPM value;
  1277. X{
  1278. X    int i;
  1279. X    int ercode = APM_OK;
  1280. X
  1281. X    APM_calc_init();
  1282. X
  1283. X    /*
  1284. X     * If stack is full, shift everything down by one, thereby losing
  1285. X     * the value at the bottom of the stack.
  1286. X     */
  1287. X    if (APM_stack_ptr >= &APM_stack[APM_STACK_SIZE]) {
  1288. X        for (i = 0; i < APM_STACK_SIZE - 1; ++i) {
  1289. X            (void)apm_assign(APM_stack[i], APM_stack[i + 1]);
  1290. X        }
  1291. X        APM_stack_ptr = &APM_stack[APM_STACK_SIZE - 1];
  1292. X    }
  1293. X
  1294. X    ercode = apm_assign(*APM_stack_ptr, value);
  1295. X    if (ercode >= APM_OK) {
  1296. X        ++APM_stack_ptr;
  1297. X    }
  1298. X
  1299. X    return (ercode);
  1300. X}
  1301. X
  1302. Xstatic int
  1303. XAPM_pop(value)
  1304. XAPM value;
  1305. X{
  1306. X    int ercode = APM_OK;
  1307. X
  1308. X    APM_calc_init();
  1309. X
  1310. X    if (APM_stack_ptr <= &APM_stack[0]) {    /* at bottom of stack */
  1311. X        ercode = apm_assign_long(value, 0L, 0, 0);
  1312. X        if (ercode >= APM_OK) {
  1313. X            APM_stack_ptr = &APM_stack[0];    /* just in case */
  1314. X        }
  1315. X    }
  1316. X    else {
  1317. X        ercode = apm_assign(value, APM_stack_ptr[-1]);
  1318. X        if (ercode >= APM_OK) {
  1319. X            --APM_stack_ptr;
  1320. X        }
  1321. X    }
  1322. X
  1323. X    return (ercode);
  1324. X}
  1325. X
  1326. Xint
  1327. Xapm_get_register(value, regnum)
  1328. XAPM value;
  1329. Xint regnum;
  1330. X{
  1331. X    APM reg;
  1332. X
  1333. X    apm_errno = APM_OK;
  1334. X
  1335. X    APM_calc_init();
  1336. X
  1337. X    if (regnum < 0 || regnum >= APM_NUM_REGISTERS) {
  1338. X        return (APM_error(APM_EPARM));
  1339. X    }
  1340. X
  1341. X    ERR_RETURN(APM_get_reg(value, regnum));
  1342. X
  1343. X    return (APM_OK);
  1344. X}
  1345. X
  1346. Xint
  1347. Xapm_set_register(previous, regnum, value)
  1348. XAPM previous;
  1349. Xint regnum;
  1350. XAPM value;
  1351. X{
  1352. X    APM reg;
  1353. X
  1354. X    apm_errno = APM_OK;
  1355. X
  1356. X    APM_calc_init();
  1357. X
  1358. X    if (regnum < 0 || regnum >= APM_NUM_REGISTERS) {
  1359. X        return (APM_error(APM_EPARM));
  1360. X    }
  1361. X
  1362. X    ERR_RETURN(APM_set_reg(previous, regnum, value));
  1363. X
  1364. X    return (APM_OK);
  1365. X}
  1366. X
  1367. Xint
  1368. Xapm_calc(va_alist)
  1369. Xva_dcl
  1370. X{
  1371. X    va_list ap;
  1372. X    APM result;
  1373. X    APM value;
  1374. X
  1375. X    va_start(ap);
  1376. X
  1377. X    apm_errno = APM_OK;
  1378. X
  1379. X    result = va_arg(ap, APM);
  1380. X    ERR_RETURN(APM_val_format(result));
  1381. X
  1382. X    APM_calc_init();
  1383. X
  1384. X    APM_calc_misc(APM_CALC_CLEAR, 0);    /* start with a clear stack */
  1385. X
  1386. X    while ((value = va_arg(ap, APM)) != NULL) {
  1387. X        if (value->magic == APM_MAGIC) {
  1388. X            ERR_RETURN(APM_push(value));
  1389. X        }
  1390. X        else if (value->magic == OP_MAGIC) {
  1391. X            int ercode = APM_calc_op(value);
  1392. X            (void)APM_free(value);    /* we must free operations */
  1393. X            ERR_RETURN(ercode);
  1394. X        }
  1395. X        else {
  1396. X            return (APM_set_errno(APM_EBADVAL));
  1397. X        }
  1398. X    }
  1399. X
  1400. X#ifdef MSDOS
  1401. X    va_end(ap);
  1402. X#else
  1403. X    va_end();
  1404. X#endif /* MSDOS */
  1405. X
  1406. X    return (APM_pop(result));
  1407. X}
  1408. X
  1409. XAPM
  1410. Xapm_op_alloc(type, operand)
  1411. Xint type;
  1412. Xint operand;
  1413. X{
  1414. X    APM op;
  1415. X
  1416. X    apm_errno = APM_OK;
  1417. X
  1418. X    op = APM_alloc();
  1419. X    if (op == (APM)NULL) {
  1420. X        APM_set_errno(APM_ENOMEM);
  1421. X        return ((APM)NULL);
  1422. X    }
  1423. X    op->magic = OP_MAGIC;
  1424. X    op->length = type;
  1425. X    op->dp = operand;
  1426. X
  1427. X    return (op);
  1428. X}
  1429. X
  1430. Xvoid
  1431. XAPM_calc_init()
  1432. X{
  1433. X    if (APM_stack_ptr == (APM *)NULL) {
  1434. X        APM local;
  1435. X        int ercode = APM_OK;
  1436. X        int i;
  1437. X
  1438. X        for (i = 0; i < TOT_REGISTERS; ++i) {
  1439. X            local = APM_alloc();
  1440. X            if (local == (APM)NULL) {
  1441. X                APM_set_errno(APM_ENOMEM);
  1442. X                break;
  1443. X            }
  1444. X            ercode = APM_parse_long(local, 0L, 0);
  1445. X            if (ercode < APM_OK) {
  1446. X                APM_set_errno(ercode);
  1447. X                break;
  1448. X            }
  1449. X            APM_register[i] = local;
  1450. X        }
  1451. X        for (i = 0; i < APM_STACK_SIZE; ++i) {
  1452. X            local = APM_alloc();
  1453. X            if (local == (APM)NULL) {
  1454. X                APM_set_errno(APM_ENOMEM);
  1455. X                break;
  1456. X            }
  1457. X            ercode = APM_parse_long(local, 0L, 0);
  1458. X            if (ercode < APM_OK) {
  1459. X                APM_set_errno(ercode);
  1460. X                break;
  1461. X            }
  1462. X            APM_stack[i] = local;
  1463. X        }
  1464. X        APM_stack_ptr = &APM_stack[0];
  1465. X    }
  1466. X}
  1467. @\END_OF_FILE_calc.c
  1468. else
  1469.   echo "shar: Will not over write calc.c"
  1470. fi
  1471. echo "Finished archive 2 of 5"
  1472. # to concatenate archives, remove anything after this line
  1473. exit 0
  1474.